/************************
 * 描述：订单列表
 * 作者：Gibil
 * 时间：2014-1-18
************************/
var OrderList = {
    companyId: '', //商家ID
    cartSiteUrl: '', //购物车站点路径
    record: 0, //总数
    scrollCount: 1, //触发获取数据的数次(等于页码)
    maxCount: 0, //可触发获取数据的总数(总页数)
    pageSize: 5, //每次触发取的记录条数
    isLoading: false, //列表是否加载中，避免重复触底加载
    memberId: '', //当前登录的会员ID

    //初始化列表
    init: function (_json) {
        if (_json.recordcount) {
            OrderList.record = _json.recordcount;
            OrderList.maxCount = _json.pagecount;
            OrderList.showHtml(_json);
        } else {
            $('#no_order_container').show();
        }

        OrderList.scrollLoad();
    },

    //滚动加载
    scrollLoad: function () {
        $(window).scroll(function () {
            //滚动高度 + 窗口高度 + (底部导航高度 + 版权块高度) >= 文档高度，注意：文档高度不包括fixed定位的元素（分类导航、底部导航）
            if ($(document).scrollTop() + $(window).height() + (50 + 50) >= $(document).height()) {
                OrderList.getMore();
            }
        });
    },

    //获取数据
    getMore: function () {
        if (OrderList.isLoading)//取数过程中，先停止重复取数
            return;

        if (OrderList.maxCount > OrderList.scrollCount) OrderList.scrollCount += 1;
        else return;

        $('#list_cloading').show(); //显示加载框
        OrderList.isLoading = true;
        setTimeout('OrderList.d()', 1000); //模拟延迟取数据
    },

    //异步获取商品列表
    d: function () {
        var dataJson = { "act": "getorderlist", "pagesize": OrderList.pageSize, "currpage": OrderList.scrollCount, "userid": OrderList.memberId, "companyid": OrderList.companyId, "sizerule": "s-80-80" };
        $.ajax({
            type: 'POST',
            url: '/ajax/public.ashx',
            dataType: 'json',
            data: dataJson,
            success: function (msg, textStatus) {
                OrderList.showHtml(msg);
                $('#list_cloading').hide(); //隐藏加载框
                OrderList.isLoading = false;
            },
            error: function (textStatus) {
                OrderList.isLoading = false;
                $('#list_cloading').hide(); //隐藏加载框
            }
        });
    },

    //添加订单列表HTML
    showHtml: function (listJson) {
        var _Html = '';
        var _list = listJson.list;

        if (_list.length > 0) {
            for (var i = 0; i < _list.length; i++) {
                if ($('#li_' + _list[i].orderid).length > 0) continue; //已存在于列表则不需要重复添加

                var _proCount = 0; //商品总数量
                var _li = '';
                _li += '<li id="li_' + _list[i].orderid + '">';
                _li += '    <div class="tab_div_title"><a href="/orderdetail.shtml?id=' + _list[i].orderid + '&comid=' + OrderList.companyId + '">订单编号<i class="maohao">:</i>' + _list[i].orderid + '</a></div>';
                _li += '    <div class="tab_img">';

                if (_list[i].items.length > 1) {//有两个以上商品只显示图片
                    _li += '    <div class="imgeslist">';
                    for (var j = 0; j < _list[i].items.length; j++) {
                        _li += '    <a><img src="' + _list[i].items[j].pic + '" alt="' + _list[i].items[j].name + '" /></a>';
                        _proCount += _list[i].items[j].num;
                    }
                    _li += '    </div>';
                } else {
                    _li += '    <div class="imgle"><a><img src="' + _list[i].items[0].pic + '" alt="' + _list[i].items[0].name + '" /></a></div>';
                    _li += '    <div class="imgri"><div class="t">';
                    _li += '        <a>' + _list[i].items[0].name + '</a>';
                    if (_list[i].items[0].skutext)
                        _li += '    <div class="format">' + _list[i].items[0].skutext + '</div>';
                    _li += '    </div></div>';
                    _proCount += _list[i].items[0].num;
                }

                _li += '        <div class="clear"></div>';
                _li += '    </div>';
                _li += '    <div class="pay">';
                _li += '        <div class="pay_span">';

                if (_list[i].status == 10) {
                    _li += '        <span class="span_left">数量<i class="maohao">:</i>' + _proCount + '&nbsp;&nbsp;总价<i class="maohao">:</i><em>￥' + _list[i].total + '</em></span>';
                    _li += '        <span class="span_ri"><a onclick="OrderList.cancel(\'' + _list[i].orderid + '\',\'' + OrderList.memberId + '\');" class="del"></a></span>';
                }
                else {
                    _li += '        <span class="span_left">';
                    switch (_list[i].status) {
                        case 20:
                            _li += '    待发货';
                            break;
                        case 30:
                            _li += '    待收货';
                            break;
                        case 40:
                            _li += '    交易完成';
                            break;
                        case 70:
                            _li += '    <span class="gray">交易关闭</span>';
                            break;
                        default: break;
                    }
                    _li += '        </span>';
                    _li += '        <span class="span_ri">数量<i class="maohao">:</i>' + _proCount + '&nbsp;&nbsp;总价<i class="maohao">:</i><em>￥' + _list[i].total + '</em></span>';
                }

                _li += '            <div class="clear"></div>';
                _li += '        </div>';

                if (_list[i].status == 10)
                    _li += '    <div class="pay_btn"><a href="' + OrderList.cartSiteUrl + '/pay.shtml?orderid=' + _list[i].orderid + '&comid=' + OrderList.companyId + '">去付款</a></div>';
                else if (_list[i].status == 30)
                    _li += '    <div class="pay_btn"><a onclick="OrderList.receipt(\'' + _list[i].orderid + '\',\'' + OrderList.memberId + '\');">确认收货</a></div>';
                _li += '    </div>';
                _li += '    <div class="clear"></div>';
                _li += '</li>';
                _Html += _li;
            }
        }
        $('#list_container').append(_Html);
    },

    //确认收货
    receipt: function (oid, mid) {
        var doReceipt = function () {
            $.ajax({
                type: 'POST',
                url: '/ajax/Public.ashx',
                dataType: 'text',
                data: { "act": "ReceiptOrder", "id": oid, "userid": mid },
                async: false,
                success: function (msg, textStatus) {
                    if (msg == "1") {
                        var $li = $('#li_' + oid);
                        $li.find('.pay_span .span_left').html('交易完成');
                        $li.find('.pay_btn').remove();
                    } else {
                        $.Alert('网络繁忙，请稍后再试...', 160);
                    }
                },
                error: function (textStatus) {
                    $.Alert('网络繁忙，请稍后再试...', 160);
                }
            });
        }
        $.Confirm.show({ content: '确定已收到货物吗?', sure: doReceipt });
    },

    //取消订单
    cancel: function (oid, mid) {
        var doCancel = function () {
            $.ajax({
                type: 'POST',
                url: '/ajax/Public.ashx',
                dataType: 'text',
                data: { "act": "CancelOrder", "id": oid, "userid": mid },
                async: false,
                success: function (msg, textStatus) {
                    if (msg == "1") {
                        $.Alert('操作成功', 160);
                        var $li = $('#li_' + oid);
                        $li.find('.pay_span .span_ri').html($li.find('.pay_span .span_left').html());
                        $li.find('.pay_span .span_left').html('<span class="gray">交易关闭</span>');
                        $li.find('.pay_btn').remove();
                    } else {
                        $.Alert('网络繁忙，请稍后再试...', 160);
                    }
                },
                error: function (textStatus) {
                    $.Alert('网络繁忙，请稍后再试...', 160);
                }
            });
        }
        $.Confirm.show({ content: '确定取消订单吗?', sure: doCancel });
    }
}